/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clases;

import java.sql.SQLException;
import org.jfree.ui.ApplicationFrame;
import java.sql.ResultSet;
import java.sql.Statement;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import javax.swing.JOptionPane;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
 *
 * @author CoLiSa
 */
public class BartChart extends ApplicationFrame{
    private conexion mdbc;
    private Statement stmt;
    
    
    public BartChart(String title){
        super(title);
        CategoryDataset dataset= crearDataset();
        JFreeChart chart=crearChart(dataset);
        ChartPanel panel = new ChartPanel(chart,false);
        panel.setPreferredSize(new Dimension(700,500));
        this.setContentPane(panel);
    }

    private CategoryDataset crearDataset() {
        abmBovino a = new abmBovino();
        try {
            a.Conectar();
            int idSenasa= 253;
            
            ResultSet rs = stmt.executeQuery("SELECT dl.peso,l.fecha, l.hora from detalle_lectura dl, bovino b, lectura l where b.idSenasa=dl.idsenasa and b.idSenasa='" + idSenasa + "' and dl.idlectura=l.idlectura order by l.fecha");
            int j = 0;
            //while (rs.next() && j < tam) {
                
                int peso = rs.getInt("peso");
                String fecha = rs.getString("fecha");
                String hora = rs.getString("hora");
                
                //tablaPeso.setValueAt(peso, j, 0);
                //tablaPeso.setValueAt(fecha, j, 1);
                //tablaPeso.setValueAt(hora, j, 2);
                //j = j + 1;
            //}
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Error aqui", JOptionPane.ERROR_MESSAGE);
        }

       
        //Nombre de las filas
        String serie1 ="Peso";
        //String serie2 ="Segundo";
        //String serie3 ="Tercero";
        //Nombre de las columnas
        String categoria1="1º Cuatrimestre";
        String categoria2="2º Cuatrimestre";
        String categoria3="3º Cuatrimestre";
        String categoria4="4º Cuatrimestre";
        //String categoria5="Categoria5";

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        //Aquí vamos a llenar nuestra data
        //pero esta data la podemos pasar por parámetros
        dataset.addValue(100.00, serie1,categoria1);
        dataset.addValue(155.32, serie1,categoria2);
        dataset.addValue(230.56, serie1,categoria3);
        dataset.addValue(350.76, serie1,categoria4);
        //dataset.addValue(350.52, serie1,categoria5);
/*
        dataset.addValue(5.0, serie2, categoria1);
        dataset.addValue(7.0, serie2, categoria2);
        dataset.addValue(6.0, serie2, categoria3);
        dataset.addValue(8.0, serie2, categoria4);
        dataset.addValue(4.0, serie2, categoria5);

        dataset.addValue(4.0, serie3, categoria1);
        dataset.addValue(3.0, serie3, categoria2);
        dataset.addValue(2.0, serie3, categoria3);
        dataset.addValue(3.0, serie3, categoria4);
        dataset.addValue(6.0, serie3, categoria5);

        dataset.addValue(1.0, serie1,categoria1);
        dataset.addValue(4.0, serie1,categoria2);
        dataset.addValue(3.0, serie1,categoria3);
        dataset.addValue(5.0, serie1,categoria4);
        dataset.addValue(5.0, serie1,categoria5);
 */
        return dataset;
    }

    private JFreeChart crearChart(CategoryDataset data) {
        JFreeChart chart= ChartFactory.createBarChart(
            "Historico del peso del animal",        //Nombre de la gráfica
            "Cuatrimestre",               //Nombre del eje Horizontal
            "Peso",                  //Nombre del eje vertical
            data,                       //Data
            PlotOrientation.VERTICAL, //Orientación
            true,                       //Incluir leyenda
            true,                       //Información al pasar el mouse
            true);                      //URls
        //Dar color al fondo del panel
        chart.setBackgroundPaint(Color.WHITE);
        //Dar color a cada categoria
        CategoryPlot plot =(CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.BLUE);
        //Color del fondo del gráfico
        plot.setBackgroundPaint(Color.WHITE);
        //Lineas divisorias
        plot.setDomainGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.BLACK);
        //Calculo de los valores en el eje x
        final NumberAxis rangeAxis=(NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        //Dar color a cada barra
        GradientPaint gp0= new GradientPaint(0.0f,0.0f,Color.blue,0.0f,0.0f,new Color(0,0,64));
        GradientPaint gp1= new GradientPaint(0.0f,0.0f,Color.green,0.0f,0.0f,new Color(0,64,0));
        GradientPaint gp2= new GradientPaint(0.0f,0.0f,Color.red,0.0f,0.0f,new Color(64,0,0));
        renderer.setSeriesPaint(0,gp0);
        renderer.setSeriesPaint(1,gp1);
        renderer.setSeriesPaint(2,gp2);

        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(
                CategoryLabelPositions.createUpRotationLabelPositions(Math.PI/6.0));
        return chart;
    }
    
    
    public static void main(String[] args)
    {
        BartChart demo1 = new BartChart("BarChartDemo01");
        demo1.pack();
        RefineryUtilities.centerFrameOnScreen(demo1);
        demo1.setVisible(true);
    }   

  

}
